/**
 * @(#)TaskInstanceDescriptionTag.java 2010-3-2
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.web.taglibs;

import javax.servlet.jsp.JspWriter;

import com.wilmar.process.core.services.api.ISystemProcessDefinitionService;
import com.wilmar.process.core.services.api.ISystemProcessInstanceService;
import com.wilmar.process.core.services.api.ISystemTaskInstanceService;
import com.wilmar.process.enumerate.EnumProcessBeanIdSet;

/**
 * 用来显示任务实例的描述信息
 * 
 * @author Administrator Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class ProcessDescriptionTag extends ProcessTagSupport {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -1486961734180928737L;

	private String type;
	public static final String DESCRIPTION_TYPE_PROCESS = "process";
	public static final String DESCRIPTION_TYPE_TASK = "task";

	/**
	 * @see org.springframework.web.servlet.tags.RequestContextAwareTag#doStartTagInternal()
	 */
	protected int doStartTagInternal() throws Exception {
		String description = "";
		if (this.type != null
				&& this.type.trim().equalsIgnoreCase(DESCRIPTION_TYPE_TASK)) {
			String tiid = super.getTaskInstanceId();
			ISystemTaskInstanceService systemTaskInstanceService = (ISystemTaskInstanceService) super
					.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_TASKINSTANCE_SERVICE);
			description = systemTaskInstanceService.getTaskDescription(tiid);
		} else {
			String piid = super.getProcessInstanceId();
			if (piid != null & piid.trim().length() != 0) {
				ISystemProcessInstanceService systemProcessInstanceService = (ISystemProcessInstanceService) super
						.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_PROCESSINSTANCE_SERVICE);
				description = systemProcessInstanceService
						.getProcessDescription(piid);
			} else if (super.getProcessDefinitionId() != null) {
				ISystemProcessDefinitionService systemProcessDefinitionService = (ISystemProcessDefinitionService) super
						.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_PROCESSDEFINITION_SERVICE);
				description = systemProcessDefinitionService
						.getProcessDescriptionById(super
								.getProcessDefinitionId().trim());
			}
		}
		JspWriter out = this.getOut();
		out.write(description);
		return SKIP_BODY;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}
}
